package com.flmes.base.config;

import com.flmes.base.base.exception.BusinessException;
import com.flmes.base.base.exception.SystemException;
import com.flmes.base.base.exception.TokenException;
import com.flmes.base.base.result.Result;
import com.flmes.base.base.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: flmes
 * @Date: 2024/10/9 22:06
 */
@Slf4j
@RestControllerAdvice
public class SystemExceptionHandler {

    /**
     * 参数错误
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class, MissingServletRequestParameterException.class})
    public Result handleMethodArgumentNotValidException(Exception e) {
        log.error("参数检验失败", e);
        if (e instanceof BindException) {
            FieldError fieldError = ((BindException) e).getBindingResult().getFieldError();
            return Result.fail(ResultCode.PARAMETER_ERROR, fieldError.getField() + fieldError.getDefaultMessage());
        } else {
            return Result.fail(ResultCode.PARAMETER_ERROR, e.getMessage());
        }
    }

    /**
     * 400
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("参数解析失败", e);
        return Result.fail(ResultCode.NOT_ACCEPTABLE);
    }

    /**
     * 405
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("不支持的请求方式", e);
        return Result.fail(ResultCode.METHOD_NOT_ALLOWED);
    }

    /**
     * 不支持的媒体格式
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Result handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        log.error("不支持的媒体类型", e);
        return Result.fail(ResultCode.NOT_ACCEPTABLE);
    }

    /**
     * 无效的token
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(TokenException.class)
    public Result handleTokenException(TokenException e) {
        log.error("无效的token");
        return Result.fail(ResultCode.UNAUTHORIZED);
    }

    /**
     * Api业务异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e) {
        log.error("业务异常:{}", e.getMessage());
        return Result.fail(e.getInfo());
    }

    /**
     * Api系统异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(SystemException.class)
    public Result handleSystemException(SystemException e) {
        log.error("系统异常:{}", e.getMessage());
        return Result.fail(e.getInfo());
    }

    /**
     * 其它未知异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result handlerException( Exception e) {
        log.error("未知异常", e);
        return Result.fail(ResultCode.ERROR);
    }

}
